package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaVersion;
import com.wudl.govern.service.MetaVersionService;
import com.wudl.govern.vo.MetaVersionVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * 元数据版本管理 Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaVersion")
public class MetaVersionController {

    @Autowired
    private MetaVersionService metaVersionService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaVersionVO>> page(@RequestBody MetaVersionVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaVersionVO> MetaVersionVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaVersion> MetaVersionList = metaVersionService.page(page, size);

        for (MetaVersion metaVersionTmp:MetaVersionList )
        {
            MetaVersionVO metaVersionVO = new MetaVersionVO();
            BeanUtil.copyProperties(metaVersionTmp, MetaVersionVO.class);
            MetaVersionVoList.add(metaVersionVO);
        }
        return CommonResult.success(MetaVersionVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaVersionVO> get(@PathVariable Integer id) {
        MetaVersion entity = metaVersionService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaVersionVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaVersionVO addVO) {
        metaVersionService.save(BeanUtil.copyProperties(addVO, MetaVersion.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaVersionVO editVO) {
        metaVersionService.updateById(BeanUtil.copyProperties(editVO, MetaVersion.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Integer id) {
        metaVersionService.removeById(id);
        return CommonResult.success();
    }

}
